#ifndef METADATA_MANAGER_H
#define METADATA_MANAGER_H

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

// 元数据管理器 - 类似MySQL的information_schema
typedef struct metadata_manager metadata_manager_t;

// 数据库信息结构
typedef struct {
    char name[64];
    char path[256];
    uint64_t created_time;
    bool is_active;
} database_info_t;

// 表信息结构
typedef struct {
    char database_name[64];
    char table_name[64];
    char table_path[256];
    uint32_t column_count;
    uint64_t row_count;
    uint64_t next_row_id;      // 下一个可用的行ID
    uint64_t created_time;
    uint64_t modified_time;
    bool is_active;
} table_info_t;

// 列信息结构
typedef struct {
    char database_name[64];
    char table_name[64];
    char column_name[64];
    uint32_t column_index;
    uint32_t data_type;        // 对应 data_type_t
    uint32_t max_length;
    bool is_primary_key;
    bool is_unique;
    bool is_not_null;
    bool is_auto_increment;
    char default_value[256];
} column_info_t;

// 索引信息结构
typedef struct {
    char database_name[64];
    char table_name[64];
    char index_name[64];
    char column_names[512];    // Comma-separated column names
    uint32_t column_count;
    bool is_unique;
    bool is_primary;
    uint64_t created_time;
    bool is_active;
} index_info_t;

// 视图信息结构
typedef struct {
    char database_name[64];
    char view_name[64];
    char select_sql[2048];     // The SELECT statement that defines the view
    char column_names[512];    // Comma-separated column names from SELECT
    uint32_t column_count;
    bool or_replace;           // Whether created with OR REPLACE
    uint64_t created_time;
    uint64_t modified_time;
    bool is_active;
} view_info_t;

// 元数据管理器结构
struct metadata_manager {
    char data_dir[256];
    char system_dir[256];
    
    // 缓存的元数据
    database_info_t* databases;
    uint32_t database_count;
    
    table_info_t* tables;
    uint32_t table_count;
    
    column_info_t* columns;
    uint32_t column_count;
    uint32_t column_capacity;
    
    index_info_t* indexes;
    uint32_t index_count;
    uint32_t index_capacity;
    
    view_info_t* views;
    uint32_t view_count;
    uint32_t view_capacity;
    
    bool is_initialized;
};

// 元数据管理器接口
metadata_manager_t* metadata_manager_create(const char* data_dir);
void metadata_manager_destroy(metadata_manager_t* meta);
int metadata_manager_init(metadata_manager_t* meta);

// 数据库操作
int metadata_add_database(metadata_manager_t* meta, const char* db_name);
int metadata_remove_database(metadata_manager_t* meta, const char* db_name);
int metadata_list_databases(metadata_manager_t* meta, database_info_t** databases, uint32_t* count);
bool metadata_database_exists(metadata_manager_t* meta, const char* db_name);

// 表操作
int metadata_add_table(metadata_manager_t* meta, const char* db_name, const char* table_name, 
                      column_info_t* columns, uint32_t column_count);
int metadata_remove_table(metadata_manager_t* meta, const char* db_name, const char* table_name);
int metadata_list_tables(metadata_manager_t* meta, const char* db_name, table_info_t** tables, uint32_t* count);
bool metadata_table_exists(metadata_manager_t* meta, const char* db_name, const char* table_name);

// 列操作
int metadata_get_table_columns(metadata_manager_t* meta, const char* db_name, const char* table_name,
                              column_info_t** columns, uint32_t* count);
int metadata_add_column(metadata_manager_t* meta, const char* db_name, const char* table_name,
                       const column_info_t* column);
int metadata_remove_column(metadata_manager_t* meta, const char* db_name, const char* table_name,
                          const char* column_name);
int metadata_modify_column(metadata_manager_t* meta, const char* db_name, const char* table_name,
                          const char* column_name, const column_info_t* new_column_info);
int metadata_rename_column(metadata_manager_t* meta, const char* db_name, const char* table_name,
                          const char* old_column_name, const char* new_column_name);

// 索引操作
int metadata_add_index(metadata_manager_t* meta, const char* db_name, const char* table_name,
                      const index_info_t* index);
int metadata_remove_index(metadata_manager_t* meta, const char* db_name, const char* index_name);
int metadata_list_indexes(metadata_manager_t* meta, const char* db_name, const char* table_name,
                         index_info_t** indexes, uint32_t* count);
bool metadata_index_exists(metadata_manager_t* meta, const char* db_name, const char* index_name);

// 视图操作
int metadata_add_view(metadata_manager_t* meta, const char* db_name, const view_info_t* view);
int metadata_remove_view(metadata_manager_t* meta, const char* db_name, const char* view_name);
int metadata_list_views(metadata_manager_t* meta, const char* db_name, view_info_t** views, uint32_t* count);
bool metadata_view_exists(metadata_manager_t* meta, const char* db_name, const char* view_name);
int metadata_get_view_definition(metadata_manager_t* meta, const char* db_name, const char* view_name,
                                view_info_t* view);

// 数据统计更新
int metadata_update_row_count(metadata_manager_t* meta, const char* db_name, const char* table_name, 
                             uint64_t row_count);

// 持久化操作
int metadata_save_to_disk(metadata_manager_t* meta);
int metadata_load_from_disk(metadata_manager_t* meta);

// 实用函数
const char* metadata_get_table_path(metadata_manager_t* meta, const char* db_name, const char* table_name);
int metadata_get_next_row_id(metadata_manager_t* meta, const char* db_name, const char* table_name);
uint64_t metadata_get_table_row_count(metadata_manager_t* meta, const char* db_name, const char* table_name);

#endif // METADATA_MANAGER_H